package theSecondWay;

import java.util.Random;

public class ThreadPush2 implements Runnable{
    public int b = 1;
    Random random;
    int i =0;

    public ThreadPush2(){
        random = new Random();
    }

    @Override
    public void run() {
        do {
            System.out.println(i+"ThreadPush Start:");
            MainClass.flag.set(b,true);
            while (MainClass.flag.get(0)) {
//              busy waiting
            }
//        critical section start , 产生随机数并入栈
            int a = random.nextInt(100);
            MainClass.stack.push(a);
            System.out.println(i+"已把随机数" + a + " 入栈");
//        critical section end,让出临界区
            MainClass.flag.set(b,false);
            i++;
        } while (i < 1000);
    }
}
